package com.yidu.logisticssystem.utils;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * <p>@Description: 常用的工具类</p>
 * <p>@Author 陈子睿</p>
 * <p>@Date: 2020-11-17</p>
 * <p>@Time: 9:00</p>
 */
public class Tools {

    /**
     * 传入一个String,判断该对象是空
     * @param obj
     * @return 如果为空返回true,否则返回false
     */
    public static boolean isEmpty(String obj){
        return obj == null || "".equals(obj);
    }

    /**
     * 传入一个String,判断该对象不是空
     * @param obj
     * @return 如果不为空返回true,否则返回false
     */
    public static boolean noEmpty(String obj){
        return obj != null && !"".equals(obj);
    }

    /**
     * 随机数
     */
    static Random rd = new Random();

    public static String getRandom(){
        long time = System.currentTimeMillis();

        time = time/(rd.nextInt(10)+1);

        int num = rd.nextInt(10000000);

        String str = time+"_"+num ;
        return str ;
    }


    /**
     * 返回AYUI需要的格式
     */
    public static Map<String,Object> getLayUiMap(List list){
        Map<String,Object> map  = new HashMap<>();
        map.put("msg","");
        map.put("code",0);
        map.put("data",list);
        map.put("count",0);
        return map;
    }


    /**
     * 返回AYUI需要的格式，自动分页
     * @param list
     * @param page
     * @param limit
     * @return
     */
    public static Map<String,Object> getLayUiMap2(List list,int page,int limit){
        PageHelper.startPage(page, limit);
        Map<String,Object> map  = new HashMap<>();
        PageInfo pageInfo = new PageInfo(list);
        map.put("msg","");
        map.put("code",0);
        map.put("data",pageInfo.getList());
        map.put("count",pageInfo.getTotal());
        return map;
    }



    public static String json1( Object obj){
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd  hh:mm:ss").create();
        String obj1 = gson.toJson(obj);
        System.out.println("来自工具传的值" + obj1 + "工具值结束");
        return obj1;
    }


}
